// pages/oa/approval/list.js
var pagetitle = ["业务审批列表", "用印审批列表", "财务审批列表", "人事审批列表", "行政待审批列表"];
var api = require("../../../utils/api.js");
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    showid: 0,
    pageSize: 10,
    searchName: "",
    examine_type: "",

    oaExamList: [],
    pageNum: 1,
    hasMoreData: true,

    /**审批中数据 */
    pageNum1: 1,
    oaExamList1: [],
    hasMoreData1: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var titleid = options.id;
    var that = this;

    wx.setNavigationBarTitle({
      title: pagetitle[titleid]
    });
    var uid = wx.getStorageSync('userId');
    if (titleid != "" && titleid != "undefined") {
      that.setData({
        showid: titleid,
        uid: uid,
      });
    }
    if (titleid == "0") {
      that.setData({
        examine_type: "3,4,5,6",
      });
    }
    if (titleid == "1") {
      that.setData({
        examine_type: '1',
      });
    }
    if (titleid == "2") {
      that.setData({
        examine_type: '13,2,14,12',
      });
    }
    if (titleid == "3") {
      that.setData({
        examine_type: '9,15',
      });
    }
    if (titleid == "4") {
      that.setData({
        examine_type: '7,8,11,10',
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      pageNum1 : 1,
      pageSize : 10,
      oaExamList1 : new Array()
    })
    this.getoaExamineList1();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    //  console.info(ssss);
    wx.showNavigationBarLoading() //在标题栏中显示加载
    that.setData({
      titleTips: "正在刷新",
      isHideLoadMore1: false,
    });
    that.data.pageNum1 = 1
    that.getoaExamineList1('正在刷新数据')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasMoreData1) {
      that.getoaExamineList1('加载更多数据')
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**搜索 */
  searchByName: function (e) {
    var that = this;
    this.setData({
      pageNum1: 1,
      pageSize: 10,
      searchName: e.detail.value,
    })
    that.getoaExamineList1('正在加载中...')
  },
  inputTyping: function (e) {
    this.setData({
      searchName: e.detail.value,
    })
  },
  clearInput: function () {
    var that = this;
    this.setData({
      pageNum1: 1,
      searchName: ""
    });
    that.getoaExamineList1('正在加载中...')
  },
  showdetail: function (e) {
    var that = this;
    var caseId = e.currentTarget.dataset.caseid;
    var stateId = e.currentTarget.dataset.stateid;
    var oaId = e.currentTarget.dataset.oaid;
    var examine_type = that.data.examine_type;
    wx.navigateTo({
      url: 'details?caseId=' + caseId + '&examine_type=' + examine_type + '&stateId=' + stateId + '&oaId=' + oaId
    });
  },
  /**得到审批中数据列表 */
  getoaExamineList1: function () {
    var that = this;
    //  console.info(that.data.examine_type);
    wx.showLoading({
      title: "数据加载中...",
    })
    api.fetchApi2("oaApproval/oaExamineCenterList", {
      examineType: that.data.examine_type,
      orgid: wx.getStorageSync("companyId"),
      userId: that.data.uid,
      pageNum: that.data.pageNum1,
      pageSize: that.data.pageSize,
      searchKey: that.data.searchName,
    }, "POST").then(res => {
      wx.hideLoading();
      var datas = res.data.data;
      var pageNums = parseInt(res.data.count) / parseInt(that.data.pageSize);
      var oaExamList1 = that.data.oaExamList1;
      if (that.data.pageNum1 == 1) {
        oaExamList1 = []
      }
      for (let i = 0; i < datas.length; i++) {
        if (datas[i]["contract_number"] == "" || datas[i]["contract_number"] == undefined) {
          datas[i]["contract_number"] = "非案件类型";
        }
        if (datas[i]["stateCreateTime"] != "" && datas[i]["stateCreateTime"] != undefined) {
          datas[i]["stateCreateTime"] = util.formatTimeDay(new Date(datas[i]["stateCreateTime"]));
        }
        var status = datas[i]["examineTypeName"]
        datas[i]["status"] = status;
        //      console.info(datas[i]);
        oaExamList1.push(datas[i]);
      }
      if (that.data.pageNum1 >= pageNums) {
        that.setData({
          oaExamList1: oaExamList1,
          hasMoreData1: false
        })
      } else {
        that.setData({
          oaExamList1: oaExamList1,
          hasMoreData1: true,
          pageNum1: that.data.pageNum1 + 1
        })
      }
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },
})

function examineTypeStatus(status) {
  if (status == '1') {
    return "用印申请";
  } else if (status == '2') {
    return "发票申请";
  } else if (status == '3') {
    return "费用减免申请";
  } else if (status == '4') {
    return "退费申请";
  } else if (status == '5') {
    return "结案申请";
  } else if (status == '6') {
    return "立案申请";
  }
  return "";
}